const path = require('path');
const express = require('express')
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
const server = require('http').createServer(app);
// 默认是不支持跨域的, 除非你显示配置
// https://socket.io/docs/v4/server-initialization/#cors
const io = require("socket.io")(server,{cors:true});
io.on("connection", socket => {
  //The API on the server-side is similar,
  //you also get a socket object which extends the Node.js EventEmitter class: https://nodejs.org/docs/latest/api/events.html#events_class_eventemitter

  // either with send()
  socket.send("Hello!");

  // or with emit() and custom event names
  socket.emit("greetings", "Hey!", { "ms": "jane" }, Buffer.from([4, 3, 3, 1]));

  // handle the event sent with socket.send()
  socket.on("message", (data) => {
    console.log('--- --- ---socket.on message--- --- ---');
    console.log(data);
  });

  // handle the event sent with socket.emit()
  socket.on("salutations", (elem1, elem2, elem3) => {
    console.log('--- --- ---socket.on salutations--- --- ---');
    console.log(elem1, elem2, elem3);
    console.log(typeof elem1, typeof elem2, typeof elem3);
  });
});

server.listen(3000);

